package com.wink_172.library.activity

import android.app.Activity
import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.wink_172.library.R
import com.wink_172.library.databinding.ActivityDownLoadBinding
import com.wink_172.library.model.Constants
import com.wink_172.library.utils.AppUtils
import com.wink_172.library.utils.CommonJavaUtils
import com.wink_172.library.utils.CommonUtils
import com.wink_172.library.view.MySeekBar
import org.xutils.x
import java.io.File




/**
 * apk下载页面载体
 * 把apk下载到data/data目录可以不需要存储权限
 *安卓7.0以上需要在在Manifest.xml里的application里，设置provider路径 请原版复制<provider **  </provider>的内容，不要任何改动
 * 要保证build.gradle的applicationId  和  代码包名保持一致
 *
 *
 */
class DownAppActivity : SmartActivity() {
    lateinit var binding: ActivityDownLoadBinding

    companion object {

//        val save_path= CommonJavaUtils.getCachePath(x.app())+"bbc.apk"
//
//        DownAppActivity.startActivity(activity,"https://fun4demo-1254407438.cos.ap-guangzhou.myqcloud.com/temp_fd/app-debug.apk",save_path,"下载页面","下载失败","下载进度(")

        @JvmStatic
        fun startActivity(context: Activity, vararg args: Any?) {
            val intent = Intent(context, DownAppActivity::class.java).apply {
                putExtra(Constants.PARAM_DATA1, args[0] as String?) //下载地址
                putExtra(Constants.PARAM_DATA2, args[1] as String?) //   data/data  目录
                putExtra(Constants.PARAM_DATA3, args[2] as String?) //   标题
                putExtra(Constants.PARAM_DATA4, args[3] as String?) //  "文字描述  下载失败 "
                putExtra(Constants.PARAM_DATA5, args[4] as String?) //  "文字描述  下载进度("
            }

            context.startActivity(intent)
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setStatusBarLightMode()
        binding = DataBindingUtil.setContentView(this, R.layout.activity_down_load)

        initView()

        init()

    }



    fun startDown() {
        val web_url = intent.getStringExtra(Constants.PARAM_DATA1)
        val save_path = intent.getStringExtra(Constants.PARAM_DATA2)  //把apk下载到data/data目录可以不需要存储权限


        val str3 = intent.getStringExtra(Constants.PARAM_DATA3)
        val str4 = intent.getStringExtra(Constants.PARAM_DATA4)
        val str5 = intent.getStringExtra(Constants.PARAM_DATA5)
//        Log.e(TAG, "init: bbc====>>save_path:"+save_path )
//        Log.e(TAG, "init: bbc====>>web_url:"+web_url )

        val progressDialog = Dialog(activity, R.style.dialog)
        progressDialog.setContentView(R.layout.progress_dialog)
        progressDialog.setCanceledOnTouchOutside(false)
        progressDialog.setCancelable(false)
        progressDialog.show()

        val title=progressDialog.findViewById<TextView>(R.id.title)
        val hint=progressDialog.findViewById<TextView>(R.id.hint)
        val progress=progressDialog.findViewById<MySeekBar>(R.id.progress)
        title.setText(str3)

//        val save_path = Constants.savePath + FileUtil.getFileUrlName(web_url) //本地保存绝对路径

        //请注意一定要先有存储权限
        CommonUtils.downLoadFile(web_url, save_path,
            object :
                com.wink_172.library.callback.ICallback {
                override fun onSendEvent(event: Int, vararg args: Any?) {//这个callback用的非常巧妙

                    when (event) {
                        Constants.EventEnum.网络请求成功.ordinal-> {
                            val file = File(save_path)
                            AppUtils.installingAPK(activity, file, x.app().packageName)

                            progressDialog.dismiss()
                            activity.finish()
                        }
                        Constants.EventEnum.网络请求错误.ordinal -> {
                            progressDialog.dismiss()
                            CommonUtils.showToast(str4)
                            activity.finish()
                        }

                        Constants.EventEnum.下载进度更新.ordinal-> {
                            val all_size= args[0] as Long
                            val current_size= args[1] as Long
                            val processPP=(current_size*100/all_size).toInt()


                            hint.setText(str5+processPP+"/100 )")
                            progress.currentProgress=processPP
                            progress.maxProgress=100
                            progress.invalidate()
                        }


                    }
                }
            }
        )
    }

    private fun initView() {}
    private  val TAG = "DownAppActivity"

    /**
     * 把apk下载到data/data目录可以不需要存储权限
     * */
    private fun init() {
        val save_path = intent.getStringExtra(Constants.PARAM_DATA2)  //把apk下载到data/data目录可以不需要存储权限

//        val file = File(save_path)
//        if(file!=null&&file.exists()){
//            AppUtils.installingAPK(activity, file, x.app().packageName)
//           finish()
//
//        }else{
            startDown()
//        }

    }


}